package m.jclecai.com.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.adapter.AllRedPkgAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.data.Lecai_SDK;
import m.jclecai.com.data.ReceivedData;
import m.jclecai.com.fragment.RmGameItemFragment;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.rounded.RoundedImageView;

/**
 * Created by ZD on 2017/7/19.
 * 名家详情
 */

public class RmFamousActivity extends BaseActivity {

    @BindView(R.id.rgTab)
    RadioGroup rgTab;
    @BindView(R.id.iv_icon)
    RoundedImageView ivIcon;
    @BindView(R.id.vp_view_pager)
    ViewPager viewPager;

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_type)
    ImageView ivType;
    @BindView(R.id.iv_position)
    ImageView ivPosition;
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.tv_speciality1)
    TextView tv_speciality1;
    @BindView(R.id.tv_speciality2)
    TextView tv_speciality2;

    AllRedPkgAdapter adapter;
    List<BaseFragment> pages;
    ReceivedData.ReconmentPersonDetail reconmentPersonDetail = null;


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 12345678:
                    initValues();
                    break;
            }
        }
    };

    @Override
    protected int getContentView() {
        return R.layout.activity_rm_famous;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("名家详情");
        leftOptionText("");
        sendReq(Lecai_SDK.persion_id);
        rgTab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.rb_one){
                    viewPager.setCurrentItem(0);
                }else  if(checkedId == R.id.rb_two){
                    viewPager.setCurrentItem(1);
                }else {
                    viewPager.setCurrentItem(2);
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        pages = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
//            RmFamousItemFragment redPkgFragment = new RmFamousItemFragment();
//            Bundle bundle = new Bundle();
//            bundle.putInt("flag", i);
//            redPkgFragment.setArguments(bundle);
//            pages.add(redPkgFragment);

            RmGameItemFragment gameItemFragment = new RmGameItemFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("flag", i);
            gameItemFragment.setArguments(bundle);
            pages.add(gameItemFragment);
        }
        adapter = new AllRedPkgAdapter(getSupportFragmentManager(), pages, null);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ((RadioButton)rgTab.getChildAt(position)).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    /**
     *  数据请求
     *  名人详情
     */
    private void sendReq(String id) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("id",id);
        Constant_Url.HTTP_URL = MyApplication.HTTP_URL_RECOMMEND;
//        showDialog();
        HttpManager.postAsyn(Constant_Url.APP_PERSON_DETAIL_URL, new HttpManager.StringCallback() {

            @Override
            public void onFailure(Request request, IOException e) {
//                dissDialog();
                DisPlay("网络异常！！！");
            }

            @Override
            public void onResponse(String response) {
//                dissDialog();
                if (!TextUtils.isEmpty(response)) {
                    Gson gson = new Gson();
                    reconmentPersonDetail =
                            gson.fromJson(response, ReceivedData.ReconmentPersonDetail.class);
                    Lecai_SDK.reconmentPersonDetail = reconmentPersonDetail;
                    Lecai_SDK.from = "recomentdetail";
                    Message msg = new Message();
                    msg.what = 12345678;
                    handler.sendMessage(msg);
                }
            }
        },paramsMap);
    }


    /**
     *  绑定数据
     */
    private void initValues(){
        Glide.with(RmFamousActivity.this).
                load(reconmentPersonDetail.user_face).
                asBitmap().
                placeholder(R.drawable.my_head_de).
                into(ivIcon);
        tvName.setText(reconmentPersonDetail.username);
        tv_content.setText(reconmentPersonDetail.content+"\n");
        int jtype = 0;
        ivType.setImageResource(jtype == 0 ? R.mipmap.home_living_football : R.mipmap.home_living_basketball);
        ivPosition.setImageResource(R.mipmap.famous_zqmj);
        String tags = reconmentPersonDetail.tags;
        if (!TextUtils.isEmpty(tags)){
            String[] split = tags.split("、");
            if(null!=split&&split.length>0){
                    tv_speciality1.setText(split[0]);
                    tv_speciality2.setText(split[1]);
            }


        }

    }

}
